
package com.prp.keywords_search_statistic;

import java.util.List;

import org.hibernate.HibernateException;

import com.prp.db.DAOFactory;
import com.prp.db.HibernateSessionFactory;
import com.prp.db.idao.IKeywordsLibDAO;
import com.prp.db.idao.ISearchRecordDAO;
import com.prp.db.idao.ISubjectRecommendationDAO;
import com.prp.db.idao.ITempKeywordDAO;
import com.prp.db.po.KeywordsLib;
import com.prp.db.po.SearchRecord;
import com.prp.db.po.SubjectRecommendation;
import com.prp.db.po.TempKeyword;

public class DatabaseProxy {
	/**
	 * 提取min(size,记录数)条关键字记录
	 * 
	 * @param size
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<KeywordsLib> loadData(int size) {
		IKeywordsLibDAO dao = DAOFactory.getDAOFactory().getKeywordsLibDAO();
		org.hibernate.Transaction tx = null;
		List<KeywordsLib> list = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			list = (List<KeywordsLib>)dao.findPart(size);
		} catch (HibernateException e) {
			if(tx != null)
				tx.rollback();
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 得到keyword所属的子类别
	 * 
	 * @param keyword
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<KeywordsLib> findKeyword(String keyword) {
		IKeywordsLibDAO dao = DAOFactory.getDAOFactory().getKeywordsLibDAO();
		org.hibernate.Transaction tx = null;
		List<KeywordsLib> list = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			list = (List<KeywordsLib>)dao.findByKeyWord(keyword);
		} catch (HibernateException e) {
			if(tx != null)
				tx.rollback();
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 将数据库中没有的关键字加入到数据库的EctKeyword表中，待管理员手工划分子类别，并将其加入关键字表
	 */
	public static void insertEtcKeyword(String keyword) {
		ITempKeywordDAO dao = DAOFactory.getDAOFactory().getTempKeywordDAO();
		org.hibernate.Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			TempKeyword tk = new TempKeyword(keyword);
			TempKeyword ke = dao.findById(keyword);
			//bug已修改，如果不存在则插入
			if( ke == null){
				dao.save(tk);
				tx.commit();
			}
		} catch (HibernateException e) {
			if(tx != null)
				tx.rollback();
			e.printStackTrace();
		}
	}

	/**
	 * 将该用户对该子类别的搜索热度+1
	 * 
	 * @param userID搜索该关键字的用户
	 * @param entityID该关键字所属的子类别
	 */
	@SuppressWarnings("unchecked")
	public static void increaseHit(Integer userID, Integer categoryID) {
		ISearchRecordDAO dao = DAOFactory.getDAOFactory().getSearchRecordDAO();
		org.hibernate.Transaction tx = null;
		SearchRecord sr;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			List<SearchRecord> list = (List<SearchRecord>)dao.findByUserIdCatetoryId(userID, categoryID);
			if (list == null || list.size() == 0)
				sr = new SearchRecord(categoryID, userID, 1);
			else {
				sr = list.get(0);
				sr.setSearchFreq(sr.getSearchFreq() + 1);
			}
			dao.attachDirty(sr);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// increaseHit(1, 30);
//		List list = getSubjectPosts("a", 0, 7);
//		System.out.println(list.size());
//		List<KeywordsLib> list = loadData(50);
//		Iterator<KeywordsLib> it = list.iterator();
//		while(it.hasNext()){
//			System.out.println(it.next().getKeyWord());
//		}
	}

	/**
	 * 返回与关键字相关的主题帖,从start位置开始搜索主题帖
	 * @param keyword 用户搜索的关键字
	 * @param start 用户希望开始搜索的位置
	 * @param count 用户希望返回的关键字条数
	 * @return
	 * 注：调用者可以通过得到返回List中的最后一个元素，得到最后一条	 * 记录的id号码，然后存入session
	 */
	@SuppressWarnings("unchecked")
	public static List<SubjectRecommendation> getSubjectPosts(String keyword,
			Integer start, Integer count) {
		ISubjectRecommendationDAO dao = DAOFactory.getDAOFactory()
				.getSubjectRecommendationDAO();
		List<SubjectRecommendation> list = dao.findByKeyword("%"+keyword+"%", start, count);
		return list;
	}
}
